package com.lanou3g.ptp.api.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lanou3g.ptp.api.service.ApiStatisticalService;
import com.lanou3g.ptp.attendance.service.AttendanceService;
import com.lanou3g.ptp.entity.StatisticalAll;
import com.lanou3g.ptp.service.ApprovalService;
import com.lanou3g.ptp.student.service.StudentService;
import com.lanou3g.ptp.user.service.UserService;
import com.lanou3g.ptp.utils.PtpResult;
import com.lanou3g.ptp.utils.PtpResultUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;

@RestController
public class StatisticalController {
    @Reference
    private AttendanceService attendanceService;
    @Reference
    private ApprovalService approvalService;
    @Autowired
    private ApiStatisticalService apiStatisticalService;
    @Reference
    private StudentService studentService;
    @Reference
    private UserService userService;

    //    每个人的出勤率
    @ApiOperation("统计所有学生在某个时间段的出勤率")
    @GetMapping("/attendance")
    public PtpResult attendanceWeek(@DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                                    @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
                                    @RequestParam(value = "page", defaultValue = "1") Integer page,
                                    @RequestParam(value = "size", defaultValue = "5") Integer size,
                                    Long taskId, HttpServletResponse response) {
        try {
            System.out.println(startDate + "开始时间");
            System.out.println(endDate + "截止时间");
            StatisticalAll result = apiStatisticalService.ratio(startDate, endDate, taskId, page, size, response);
            return PtpResultUtils.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    //    每个人的早晚打卡时间
    @ApiOperation("统计所有学生在某一天的早晚两次打卡时间")
    @GetMapping("/attendance/all")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "date", paramType = "query")
    )
    public PtpResult attendance(@DateTimeFormat(pattern = "yyyy-MM-dd") Date date, Long taskId,
                                @RequestParam(value = "page", defaultValue = "1") Integer page,
                                @RequestParam(value = "size", defaultValue = "5") Integer size) {
        try {
            PtpResult ptpResult = apiStatisticalService.ratioAll(date, page, size, taskId);
            return PtpResultUtils.ok(ptpResult);
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }

    }
}

